package guda.house.web.action.weixin;

import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.util.ErrorCode;
import guda.house.web.helper.ResponseHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by well on 16/4/24.
 */
@Controller
@RequestMapping(value = "weixin")
public class AppAction {

    public static final String weixin_token="qiachengapp";

    @RequestMapping(value = "app.htm", method = RequestMethod.GET)
    public void app(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        JsonResult result = JsonResult.getInstance();
        if(signature == null || timestamp == null || nonce == null || echostr == null){
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.PARAM_MISS));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        List<String> paramList = new ArrayList<String>();
        paramList.add(timestamp);
        paramList.add(nonce);
        paramList.add(weixin_token);
        Collections.sort(paramList);

        StringBuilder buf = new StringBuilder();
        for(String s:paramList){
            buf.append(s);
        }

        String s = sha1(buf.toString());
        if(!s.equals(signature)){
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.PARAM_MISS));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

        PrintWriter out = null;
        try {
            response.setContentType("application/json;charset=UTF-8");
            out = response.getWriter();
            out.print(echostr);
            out.flush();
        } catch (IOException e) {

        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    public static String sha1(String decript) {
        try {
            MessageDigest digest = java.security.MessageDigest
                    .getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

}
